package search

import (
    "context"
    "fmt"
    "log"
    "github.com/olivere/elastic/v7"
)

var Client *elastic.Client

func InitElasticsearch() {
    var err error
    Client, err = elastic.NewClient(elastic.SetURL("http://localhost:9200"))
    if err != nil {
        log.Fatalf("Failed to connect to Elasticsearch: %v", err)
    }

    log.Println("Elasticsearch connected successfully")
}

func IndexDocument(index string, document interface{}) error {
    put, err := Client.Index().
        Index(index).
        BodyJson(document).
        Do(context.Background())
    if err != nil {
        return fmt.Errorf("failed to index document: %w", err)
    }

    log.Printf("Indexed document %s to index %s\n", put.Id, put.Index)
    return nil
}